package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.WMember;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weiriya.domain.WArea;
import com.ruoyi.weiriya.service.*;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weiriya.mapper.WNonVipSettingMapper;
import com.ruoyi.weiriya.domain.WNonVipSetting;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 非会员购买设置Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WNonVipSettingServiceImpl extends ServiceImpl<WNonVipSettingMapper, WNonVipSetting> implements IWNonVipSettingService
{

    @Autowired
    private WNonVipSettingMapper nonVipSettingMapper;

    @Autowired
    private IWAreaService areaService;

    @Autowired
    private IWCompanyService companyService;

    @Autowired
    private IWMemberService memberService;

    @Override
    public boolean checkLowestAmount(WMember member, String areaId) {
        if(StringUtils.isBlank(areaId)){
            return false;
        }
        WMember wMember = memberService.getById(member.getId());
        Double wallet = wMember.getWallet();
        // 获取当前小区的充电价格
        WArea area = areaService.getById(areaId);
        Double lowestAmount = area.getLowestAmount();
        if(Double.compare(lowestAmount, wallet) > 0){
            throw new ServiceException("预存金小于：" + lowestAmount + "元\n请先充值！", 201);
        }

        return true;
    }

    @Override
    public List<WNonVipSetting> getList(WNonVipSetting wNonVipSetting) {
        QueryWrapper<WNonVipSetting> queryWrapper = wNonVipSetting.queryWrapper();
        if(StringUtils.isNotBlank(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            List<String> areaIdListByUserId = companyService.getAreaIdList();
            if(areaIdListByUserId.size() > 0) {
                queryWrapper.in("nvs.area_id", areaIdListByUserId);
            } else {
                return new ArrayList<>();
            }
        }
         return nonVipSettingMapper.getList(queryWrapper);
    }

    @Override
    public boolean saveWNonVipSetting(WNonVipSetting wNonVipSetting) {

        if(!checkWNonVipSetting(wNonVipSetting)){
            throw new ServiceException("金额范围为：0.01 - 9999.99");
        }

        if(StringUtils.isBlank(wNonVipSetting.getAreaId())){
            throw new ServiceException("请选择点位！");
        }else {
            List<WArea> areaListByCompanyId = companyService.getAreaListByCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
            List<String> collect = areaListByCompanyId.stream().map(WArea::getId).collect(Collectors.toList());
            if(!collect.contains(wNonVipSetting.getAreaId())){
                throw new ServiceException("权限不足！");
            }
        }

        WNonVipSetting nonVipSetting = this.getOne(new LambdaQueryWrapper<WNonVipSetting>()
                .eq(WNonVipSetting::getAreaId, wNonVipSetting.getAreaId()));
        if(StringUtils.isNotNull(nonVipSetting)){
            throw new ServiceException("该区域的此类型收费标准已存在！");
        }
        return this.save(wNonVipSetting);
    }

    private boolean checkWNonVipSetting(WNonVipSetting wNonVipSetting) {
        Double chargeHourAmount = wNonVipSetting.getChargeHourAmount();
        if(chargeHourAmount < 0.01D || chargeHourAmount > 9999.99D){
            return false;
        }
        Double chargeDayAmount = wNonVipSetting.getChargeDayAmount();
        if(chargeDayAmount < 0.01D || chargeDayAmount > 9999.99D){
            return false;
        }
        Double stopDayAmount = wNonVipSetting.getStopDayAmount();
        if(stopDayAmount < 0.01D || stopDayAmount > 9999.99D){
            return false;
        }
        Double stopHourAmount = wNonVipSetting.getStopHourAmount();
        if(stopHourAmount < 0.01D || stopHourAmount > 9999.99D){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateWNonVipSettingById(WNonVipSetting wNonVipSetting) {

        if(!checkWNonVipSetting(wNonVipSetting)){
            throw new ServiceException("金额范围为：0.01 - 9999.99");
        }
        return this.updateById(wNonVipSetting);
    }

    @Override
    public WNonVipSetting getWNonVipSettingById(String id, Integer settingType) {
        if(1 == settingType){
            return nonVipSettingMapper.getChargeWNonVipSettingById(id);
        } else {
            return nonVipSettingMapper.getDepositWNonVipSettingById(id);
        }

    }

    @Override
    public WNonVipSetting getWNonVipSettingByAreaId(String areaId) {
        return this.getOne(new LambdaQueryWrapper<WNonVipSetting>()
                .eq(WNonVipSetting::getAreaId, areaId));
    }
}
